//
//  TreeItemCell.m
//  QRMaster
//
//  Created by lc on 16/5/29.
//  Copyright © 2016年 myApple. All rights reserved.
//

#import "TreeItemCell.h"

@interface TreeItemCell()

@property(nonatomic,strong)UILabel * nameLabel;
@property(nonatomic,strong)RoomsModel * roomInfo;
@property(nonatomic,strong)UIView * lineView;
@property(nonatomic,strong)UIImageView * roomImgView;
@end

@implementation TreeItemCell

+(instancetype)cellWithTableView:(UITableView *)tableView
{
    static NSString * identityId = @"TreeItemCell";
    TreeItemCell * cell = [tableView dequeueReusableCellWithIdentifier:identityId];
    if (!cell) {
        cell = [[TreeItemCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identityId];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
    }
    return cell;
}

- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        // Initialization code
        [self addAllSubView];
    }
    return self;
}

-(void)addAllSubView
{
    [self addSubview:self.nameLabel];
    [self addSubview:self.lineView];
    [self addSubview:self.roomImgView];
}

#pragma mark ----------initData
-(void)setData:(RoomsModel *)roomInfo
{
    self.roomInfo = roomInfo;
    self.nameLabel.text = self.roomInfo.roomName;
    self.roomImgView.image = [UIImage imageNamed:@"roomcard_blue"];
}
-(void)layoutSubviews
{
    [super layoutSubviews];
    CGFloat xOffSet = 15;
    if (self.roomInfo) {
        xOffSet = 22;
    }
    
    _roomImgView.frame = CGRectMake(xOffSet, (self.frame.size.height - 28)/2.0, 28, 28);
    
    _nameLabel.frame = CGRectMake(xOffSet + 28 + 16, (self.frame.size.height - 20)/2.0, self.frame.size.width - 16 - 90, 20);
    if (self.isLastRow) {
        self.lineView.frame = CGRectMake(0, self.frame.size.height - 0.5, self.frame.size.width, 0.5);
    }
    else
    {
        self.lineView.frame = CGRectMake(xOffSet, self.frame.size.height - 0.5, self.frame.size.width - xOffSet, 0.5);
    }
    
    float width = [self calculateWidth:self.roomInfo.roomName];
    width += self.nameLabel.frame.origin.x;
    
}

#pragma mark -----lazy

-(UIImageView *)roomImgView
{
    if (!_roomImgView) {
        _roomImgView = [[UIImageView alloc] initWithFrame:CGRectMake(22,  (self.frame.size.height - 28)/2.0, 28, 28)];
        _roomImgView.image = [UIImage imageNamed:@"roomcard_blue"];
    }
    return _roomImgView;
}

-(UILabel *)nameLabel
{
    if (!_nameLabel) {
        _nameLabel = [[UILabel alloc]init];
        _nameLabel.font = [UIFont systemFontOfSize:16.0];
    }
    return _nameLabel;
}

-(UIView *)lineView
{
    if (!_lineView) {
        _lineView = [[UIView alloc]init];
        _lineView.backgroundColor = UIColorFromRGB(0xc1c1c1);
    }
    return _lineView;
}


-(void)setIsLastRow:(BOOL)isLastRow
{
    _isLastRow = isLastRow;
}

#pragma mark -- roomNo自适应长度
-(CGFloat)calculateWidth:(NSString *)str
{
    CGSize size =CGSizeMake(300,60);
    
    NSDictionary * tdic = [NSDictionary dictionaryWithObjectsAndKeys:[UIFont systemFontOfSize:20.0],NSFontAttributeName,nil];
    
    //ios7方法，获取文本需要的size，限制宽度
    
    CGSize  actualsize =[str boundingRectWithSize:size options:NSStringDrawingUsesLineFragmentOrigin  attributes:tdic context:nil].size;
    return actualsize.width;
}

- (void)awakeFromNib {
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
